<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="type === 'edit' ? $t('编辑') : $t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" :width="lang==='zh'?'450px':'580px'" top="10vh" @close='close'>
    <div class="dialogContentBox">
      <el-form ref="form" :model="record" :label-width="lang==='zh'?'90px':'110px'" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('名称')" prop="name">
          <el-input v-emoji v-model="record.name" ref="name"  :style="lang==='zh'? 'width:240px':'width:380px'" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('是否启用存储')" prop="status">
          <el-select size="medium" v-model="record.status" ref="status" :placeholder="$t('是否启用存储')" :style="lang==='zh'? 'width:240px':'width:380px'">
            <el-option v-for="(item,index) in statusList" :id="`remote-config-dialog-status${index}`" :key="item.value" :label="$t(item.label)" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('外发格式')" prop="type">
          <el-select size="medium" v-model="record.type" ref="type" :placeholder="$t('选择外发格式')" @change="typeChange" :style="lang==='zh'? 'width:240px':'width:380px'">
            <el-option v-for="(item,index) in typeList" :id="`remote-config-dialog-type${index}`" :key="item.value" :label="$t(item.label)" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('日志级别')" prop="lv">
          <el-select size="medium" v-model="record.lv" ref="lv" :placeholder="$t('选择日志级别')" :disabled='record.type === "power"' :style="lang==='zh'? 'width:240px':'width:380px'">
            <el-option v-for="(item,index) in levelList" :id="`remote-config-dialog-lv${index}`" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('地址')" prop="addr">
          <el-input v-emoji v-model="record.addr" ref="addr" :style="lang==='zh'? 'width:240px':'width:380px'" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('端口')" prop="port">
          <el-input v-emoji v-model="record.port" ref="port" :style="lang==='zh'? 'width:240px':'width:380px'" type="number" :placeholder="$t('请输入1-65535的数字,默认514端口')" size='small'></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addServiceLog, editServiceLog } from '@/api/system/log'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'DownloadDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入名称')))
      } else if (value.length > 127) {
        callback(new Error(this.$t('输入内容不应超出长度 127 个字节')))
      } else {
        callback()
      }
    }
    var validateAddr = (rule, value, callback) => {
      let ipv4 = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
      let ipv6 = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
      let reg = /^(([-\u4E00-\u9FA5a-z0-9]{1,63})\.)+([\u4E00-\u9FA5a-z]{2,63})\.?$/
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入地址')))
      } else if (ipv4.test(value) || ipv6.test(value) || reg.test(value)) {
        callback()
      } else {
        callback(new Error(this.$t('IP格式不正确')))
      }
    }
    var validatePort = (rule, value, callback) => {
      if (value && (value < 1 || value > 65535)) {
        callback(new Error(this.$t('端口的范围为1~65535')))
      } else {
        callback()
      }
    }
    return {
      lang: this.$i18n.locale,
      dialogVisible: false,
      record: {
        name: '',
        old_name: '',
        status: 'on',
        type: 'normal',
        lv: 3,
        addr: '',
        port: ''
      },
      recordCopy: {
        name: '',
        old_name: '',
        status: 'on',
        type: 'normal',
        lv: 3,
        addr: '',
        port: ''
      },
      type: null,
      levelList: [
        { value: 1, label: 'ERROR' },
        { value: 2, label: 'ALERT' },
        { value: 3, label: 'INFO' },
        { value: 4, label: 'DEBUG' }
      ],
      typeList: [
        { value: 'normal', label: '默认' },
        { value: 'power', label: '电力格式' },
        { value: 'json', label: 'json' }
      ],
      statusList: [
        { value: 'on', label: '启用' },
        { value: 'off', label: '禁用' }
      ],
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }],
        addr: [{ required: false, validator: validateAddr, trigger: 'blur' }],
        port: [{ required: false, validator: validatePort, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data, type) {
      this.type = type
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['name', 'addr', 'port']
        const selectData = ['status', 'type', 'lv']
        inputClass(this.$refs, data)
        selectClass(this.$refs, selectData)
      })
      if (data) {
        this.record = Object.assign(this.record, data)
        this.record.old_name = data.name
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // this.record.port = Number(this.record.port)
          if (this.type === 'edit') {
            editServiceLog(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('编辑成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          } else {
            addServiceLog(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('添加成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          }
        }
      })
    },
    typeChange () {
      if (this.record.type === 'power') {
        this.record.lv = 4
      } else {
        this.record.lv = 3
      }
    },
    close () {
      this.dialogVisible = false
      this.type = null
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style scoped>
.dialogContentBox {
  max-height: calc(80vh - 184px);
  overflow-y: auto;
}
</style>
